//
//  New_HomeStatisticsCell.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2021/7/6.
//  Copyright © 2021 吕晶. All rights reserved.
//

#import "New_HomeStatisticsCell.h"
#import "HomeStatistics3PLVC.h"
#import "HomeStatisticsTaskVC.h"
#import "HomeStatisticsTaskSourceVC.h"
#import "HomeStatisticsTranMileageVC.h"

@interface New_HomeStatisticsCell ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *logoImgView;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) New_HomeStatisticsView *view1;
@property (nonatomic, strong) New_HomeStatisticsView *view2;
@property (nonatomic, strong) New_HomeStatisticsView *view3;
@property (nonatomic, strong) New_HomeStatisticsView *view4;

@end

@implementation New_HomeStatisticsCell

+ (instancetype)New_HomeStatisticsCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"New_HomeStatisticsCell";
    New_HomeStatisticsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[New_HomeStatisticsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, ScreenW - 20, 110)];
        self.bgView.backgroundColor = HexColor(0xffffff);
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 6;
        [self.contentView addSubview:self.bgView];
        
        self.height = self.bgView.nim_bottom;
        
        self.logoImgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 15, 15)];
        self.logoImgView.image = [UIImage imageNamed:@"icon_statistics"];
        [self.bgView addSubview:self.logoImgView];
        
        self.titleLab = [UILabel new];
        self.titleLab.text = @"平台资源";
        self.titleLab.textColor = HexColor(0x333333);
        self.titleLab.font = BoldFontOfSize(16);
        [self.titleLab sizeToFit];
        self.titleLab.nim_left = self.logoImgView.nim_right + 3;
        self.titleLab.nim_centerY = self.logoImgView.nim_centerY;
        [self.bgView addSubview:self.titleLab];
        
        self.view1 = [[New_HomeStatisticsView alloc] initWithFrame:CGRectMake(0, self.logoImgView.nim_bottom + 20, self.bgView.nim_width * 0.25, 50)];
        self.view1.clickBlock = ^{
            HomeStatistics3PLVC *vc = [HomeStatistics3PLVC new];
            vc.hidesBottomBarWhenPushed = YES;
            [[FactoryMethod getCurrentVC].rt_navigationController pushViewController:vc animated:YES];
        };
        [self.bgView addSubview:self.view1];
        
        self.view2 = [[New_HomeStatisticsView alloc] initWithFrame:CGRectMake(self.view1.nim_right, self.view1.nim_top, self.view1.nim_width, self.view1.nim_height)];
        self.view2.clickBlock = ^{
            HomeStatisticsTaskSourceVC *vc = [HomeStatisticsTaskSourceVC new];
            vc.hidesBottomBarWhenPushed = YES;
            [[FactoryMethod getCurrentVC].rt_navigationController pushViewController:vc animated:YES];
        };
        [self.bgView addSubview:self.view2];
        
        self.view3 = [[New_HomeStatisticsView alloc] initWithFrame:CGRectMake(self.view2.nim_right, self.view1.nim_top, self.view1.nim_width, self.view1.nim_height)];
        self.view3.clickBlock = ^{
            HomeStatisticsTaskVC *vc = [HomeStatisticsTaskVC new];
            vc.hidesBottomBarWhenPushed = YES;
            [[FactoryMethod getCurrentVC].rt_navigationController pushViewController:vc animated:YES];
        };
        [self.bgView addSubview:self.view3];
        
        self.view4 = [[New_HomeStatisticsView alloc] initWithFrame:CGRectMake(self.view3.nim_right, self.view1.nim_top, self.view1.nim_width, self.view1.nim_height)];
        self.view4.clickBlock = ^{
            HomeStatisticsTranMileageVC *vc = [HomeStatisticsTranMileageVC new];
            vc.hidesBottomBarWhenPushed = YES;
            [[FactoryMethod getCurrentVC].rt_navigationController pushViewController:vc animated:YES];
        };
        [self.bgView addSubview:self.view4];
    }
    return self;
}

-(void)setDataArray:(NSMutableArray<HomeStatisticsModel *> *)dataArray {
    _dataArray = dataArray;
    [dataArray enumerateObjectsUsingBlock:^(HomeStatisticsModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == 0) {
            self.view1.model = obj;
        }else if (idx == 1) {
            self.view2.model = obj;
        }else if (idx == 2) {
            self.view3.model = obj;
        }else if (idx == 3) {
            self.view4.model = obj;
        }
    }];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

@interface New_HomeStatisticsView ()

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *unitLab;
@property (nonatomic, strong) UILabel *contentLab;
@property (nonatomic, strong) UIButton *btn;

@end

@implementation New_HomeStatisticsView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.titleLab = [UILabel new];
        self.titleLab.textColor = HexColor(0x333333);
        self.titleLab.font = BoldFontOfSize(16);
        [self addSubview:self.titleLab];
        
        self.unitLab = [UILabel new];
        self.unitLab.textColor = HexColor(0xF36262);
        self.unitLab.font = BoldFontOfSize(12);
        [self addSubview:self.unitLab];
        
        self.contentLab = [UILabel new];
        self.contentLab.textColor = HexColor(0x999999);
        self.contentLab.font = BoldFontOfSize(12);
        [self addSubview:self.contentLab];
        
        self.btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self.btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.btn];
    }
    return self;
}

- (void)btnClick {
    if (self.clickBlock) {
        self.clickBlock();
    }
}

-(void)setModel:(HomeStatisticsModel *)model {
    _model = model;
    
    CGFloat viewW = (ScreenW - 20) * 0.25;
    self.titleLab.text = model.title;
    [self.titleLab sizeToFit];
    
    self.unitLab.text = model.unit;
    [self.unitLab sizeToFit];
    
    self.titleLab.nim_top = 0;
    self.unitLab.nim_centerY = self.titleLab.nim_centerY;
    if (self.titleLab.nim_width + self.unitLab.nim_width > viewW) {
        self.titleLab.nim_left = 0;
        
        self.titleLab.nim_width = viewW - self.unitLab.nim_width;
        self.unitLab.nim_left = viewW - self.unitLab.nim_width;
        
    }else {
        self.titleLab.nim_left = (viewW - self.titleLab.nim_width - self.unitLab.nim_width) * 0.5;
        self.unitLab.nim_left = self.titleLab.nim_right;
    }
    
    self.contentLab.text = model.content;
    [self.contentLab sizeToFit];
    self.contentLab.nim_centerX = viewW * 0.5;
    self.contentLab.nim_top = self.titleLab.nim_bottom + 10;
}

@end
